package com.fingard.dsp.bank.directbank.kjtpay01;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

public class KJTPAY01SinglePay extends KJTPAY01Base implements ISinglePay {
	@Override
	public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
		if("190802".equals(p_req1908.reqHeader.commandCode)){
			 tmpRetDetail = d_190802(p_req1908);
		}else if("190801".equals(p_req1908.reqHeader.commandCode)){
			tmpRetDetail = d_190801(p_req1908);
		} else {
			tmpRetDetail.respInfo = "不支持的交易方式";
			tmpRetDetail.respCode = "-1";
		}
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}
	
	private RetAtsSglDetail d_190802(ReqSingle p_req1908) throws Exception {
		RetAtsSglDetail retDetail = new RetAtsSglDetail(p_req1908);

		try {
			JSONObject json = new JSONObject();
			KJTPAY01Header bankHeader = new KJTPAY01Header();
			bankHeader.transCode = "transfer_to_account";
			bankHeader.version = "1.0";
			bankHeader.reqSeqID = getQryTransID();
			// 组装请求前缀
			writeCommonHeader(json, bankHeader);
			//组装请求报文体json
			JSONObject body = new JSONObject();
			body.put("out_trade_no",p_req1908.singleDetail.askNumber+"_01");
			//出款快捷通会员标识类型，
			//默认 1 1-快捷通会员 ID
			//2-快捷通会员登录号
			body.put("payer_identity_type","1");
			body.put("payer_identity",p_req1908.singleDetail.payAct);
			body.put("payer_identity_name",p_req1908.singleDetail.payName);
			body.put("payee_identity_type","1");
			body.put("payee_identity",p_req1908.singleDetail.recAct);
			body.put("payee_identity_name",p_req1908.singleDetail.recName);
			//时效 G:普通 R:实时 D:次日
			body.put("aging","R");
			body.put("transfer_amount",p_req1908.singleDetail.payAmount);
			body.put("currency","CNY");
			//业务产品码
			//10310-会员转账（实时）
			//10311-会员转账（普通）
			//10312-会员转账（次日）
			//10313-冻结转出（实时）
			//10314-转入冻结（实时）
			body.put("biz_product_code","10310");
			body.put("memo",p_req1908.singleDetail.memo);
			body.put("biz_no",p_req1908.singleDetail.abstractATS);

			String[] tmpStrRet = sendToBank(json,body.toString(), p_req1908.reqHeader.transType, p_req1908.reqHeader.transType);

			if (tmpStrRet[0].length() == 0) {
				//把结果转换成json
				JSONObject response = JSON.parseObject(tmpStrRet[1]);
				if (response != null) {
					String responseCode = response.getString("code");
					String responseMsg = response.getString("msg");
					if ("S10000".equals(responseCode)) {
						JSONObject content = JSON.parseObject(response.getString("biz_content"));
						if(content!=null){
							String repCode = response.getString("sub_code");
							String resMsg = response.getString("sub_msg");
							if(repCode!=null && "S10000".equals(repCode)) {
								retDetail.respCode = "0";
								retDetail.respInfo = "请求提交成功";
								retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
								retDetail.transState = p_req1908.reqHeader.commandCode + content.getString("status");
								retDetail.payInfoCode = retDetail.transState;
							}else{
								//通讯无异常 不存在超时响应问题 指令提交失败
								retDetail.respCode = "0";
								retDetail.respInfo = resMsg;
								retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
								retDetail.transState = repCode;
								retDetail.payInfoCode = repCode;
							}
						} else {
							retDetail.respCode = "-1";
							retDetail.respInfo = responseMsg;
						}
					} else {
						retDetail.respCode = "0";
						retDetail.respInfo = responseMsg;
						retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
						retDetail.transState = responseCode;
						retDetail.payInfoCode = responseCode;
					}

				} else {
					retDetail.respCode = "-1";
					retDetail.respInfo = "请求未正常返回";
				}
			} else if (tmpStrRet[0].equals(ConnState.FAIL)) {
				retDetail.respCode = "0";
				retDetail.respInfo = tmpStrRet[1];
				retDetail.transState = PaymentState.FAIL_Payment;
				retDetail.payInfoCode = tmpStrRet[0];
			} else {
				retDetail.respCode = "-1";
				retDetail.respInfo = tmpStrRet[1];
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retDetail.respCode = "-1";
			retDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		return retDetail;
	}

	private RetAtsSglDetail d_190801(ReqSingle p_req1908) throws Exception {
		RetAtsSglDetail retDetail = new RetAtsSglDetail(p_req1908);

		try {
			JSONObject json = new JSONObject();
			KJTPAY01Header bankHeader = new KJTPAY01Header();
			bankHeader.transCode = "entry_account_offline";
			bankHeader.version = "1.0";
			bankHeader.reqSeqID = getQryTransID();
			// 组装请求前缀
			writeCommonHeader(json, bankHeader);
			//组装请求报文体json
			JSONObject body = new JSONObject();
			body.put("out_trade_no",p_req1908.singleDetail.askNumber);
			//出款快捷通会员标识类型，
			//默认 1 1-快捷通会员 ID
			//2-快捷通会员登录号
			body.put("bank_code",p_req1908.singleDetail.payBankCode);
			body.put("amount",p_req1908.singleDetail.payAmount);
			body.put("member_id",p_req1908.singleDetail.payAct);
			body.put("biz_product_code","30101");

			String[] tmpStrRet = sendToBank(json,body.toString(), p_req1908.reqHeader.transType, p_req1908.reqHeader.transType);

			if (tmpStrRet[0].length() == 0) {
				//把结果转换成json
				JSONObject response = JSON.parseObject(tmpStrRet[1]);
				if (response != null) {
					String responseCode = response.getString("code");
					String responseMsg = response.getString("msg");
					if ("S10000".equals(responseCode)) {
						JSONObject content = JSON.parseObject(response.getString("biz_content"));
						if(content!=null){
							String repCode = response.getString("sub_code");
							String resMsg = response.getString("sub_msg");

								retDetail.respCode = "0";
								retDetail.respInfo = resMsg;
								retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
								retDetail.transState = p_req1908.reqHeader.commandCode + repCode;
								retDetail.payInfoCode = retDetail.transState;
						} else {
							retDetail.respCode = "-1";
							retDetail.respInfo = responseMsg;
						}
					} else {
						retDetail.respCode = "0";
						retDetail.respInfo = responseMsg;
						retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
						retDetail.transState = responseCode;
						retDetail.payInfoCode = responseCode;
					}

				} else {
					retDetail.respCode = "-1";
					retDetail.respInfo = "请求未正常返回";
				}
			} else if (tmpStrRet[0].equals(ConnState.FAIL)) {
				retDetail.respCode = "0";
				retDetail.respInfo = tmpStrRet[1];
				retDetail.transState = PaymentState.FAIL_Payment;
				retDetail.payInfoCode = tmpStrRet[0];
			} else {
				retDetail.respCode = "-1";
				retDetail.respInfo = tmpStrRet[1];
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retDetail.respCode = "-1";
			retDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		return retDetail;
	}

	@Override
	public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
		RetAtsSglDetail tmpRetAtsDetail = new RetAtsSglDetail(p_req1944);

		try {
			JSONObject json = new JSONObject();
			KJTPAY01Header bankHeader = new KJTPAY01Header();
			bankHeader.transCode = "trade_query";
			bankHeader.version = "1.0";
			//组装请求前缀
			writeCommonHeader(json,bankHeader);
			//组装请求报文体json
			JSONObject req = new JSONObject();
			req.put("out_trade_no",p_req1944.askNumber);
			String[] tmpStrRet = sendToBank(json,req.toString(), p_req1944.reqHeader.transType, p_req1944.reqHeader.transType);
            if (tmpStrRet[0].length() == 0) {
				//把结果转换成json
				JSONObject response = JSON.parseObject(tmpStrRet[1]);
				if (response != null) {
					String responseCode = response.getString("code");
					String responseMsg = response.getString("msg");
					if ("S10000".equals(responseCode)) {
						JSONObject content = JSON.parseObject(response.getString("biz_content"));
						String repCode = content.getString("sub_code");
						String resMsg = content.getString("sub_msg");
						String status = content.getString("status");
						tmpRetAtsDetail.reqSeqID = p_req1944.askNumber;
						tmpRetAtsDetail.respCode = "0";
						tmpRetAtsDetail.respInfo = resMsg;
						if(repCode!=null && "S10000".equals(repCode)){
							tmpRetAtsDetail.transState = p_req1944.reqHeader.commandCode + status;
							tmpRetAtsDetail.payInfoCode = status;
						} else {
							tmpRetAtsDetail.transState = repCode;
							tmpRetAtsDetail.payInfoCode = repCode;
						}
					} else{
						tmpRetAtsDetail.respCode = "-1";
						tmpRetAtsDetail.respInfo = responseMsg;
					}
				} else {
					tmpRetAtsDetail.respCode = "-1";
					tmpRetAtsDetail.respInfo = "请求未正常返回";
				}
			} else {
				tmpRetAtsDetail.respCode = "-1";
				tmpRetAtsDetail.respInfo = tmpStrRet[1];
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetAtsDetail.respCode = "-1";
			tmpRetAtsDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		retAts.writeRetDetail(tmpRetAtsDetail);
		return retAts;
	}

	@Override
	public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
		return null;
	}

	@Override
	public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
		return null;
	}
}